package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * 患者入院申请取消：DoCancelAppointInHos()
 *
 * @author liuxiaohui
 * @date 2019/3/26
 */
public class DoCancelAppointInHosTransfer extends AbstractTransfer {
    public DoCancelAppointInHosTransfer(String method, Client client, String messageMethod) {
        super(method, client, messageMethod);
    }

    // 微信公众号注册手机号：微信消息推送用
    public String wxUserMobile = "";
    // 患者姓名：微信消息推送用
    public String patientName = "";
    // 预约时间：微信消息推送用
    public String appointmentTime = "";

    /**
     * 发送：微信前端JSON数据转换为HIS用的XML数据（微信前端==》HIS）
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        // 身份证号
        String idNo = (String) params.get("idNo");
        // 患者ID
        String patientId = (String) params.get("patientId");
        // 入院申请ID
        String hosAppId = (String) params.get("hosAppId");
        // 微信公众号注册手机号：微信消息推送用
        wxUserMobile = (String) params.get("wxUserMobile");
        // 患者姓名：微信消息推送用
        patientName = (String) params.get("patientName");
        // 预约时间：微信消息推送用
        appointmentTime = (String) params.get("appointmentTime");


        // 交易ID：his的方法名
        Document document = getTplDoc(getMethod());

        // 将各个入参数设值（入参==》XML）
        Element dataRow = document.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        // 身份证号
        dataRow.addAttribute("ID_NO", idNo);
        // 患者ID
        dataRow.addAttribute("PATIENT_ID", patientId);
        // 入院申请ID
        dataRow.addAttribute("HOS_APP_ID", hosAppId);

        return document.asXML();
    }

    /**
     * 接收：HIS返回的XML数据并转换为JSON（HIS==》微信前端）
     * @param resMsg
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);

        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");

        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            // 微信公众号注册手机号：Log 输出用
            resMap.put("wxUserMobile", wxUserMobile);
            // 患者姓名：Log 输出用
            resMap.put("patientName", patientName);
            // 预约时间：Log 输出用
            resMap.put("appointmentTime", appointmentTime);

            // 返回Code
            resMap.put("code", executeStatus);
            // 返回信息
            resMap.put("message", "成功！");

            // 微信消息推送
            WeiChatSendMsg wxMsg = new WeiChatSendMsg();
            wxMsg.sendMsgDoCancelAppointInHos(wxUserMobile, patientName, appointmentTime);
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }


}
